package com.blog.controller;

import com.blog.annotation.UnderlineToCamel;
import com.blog.dao.query.UserQuery;
import com.blog.form.UserForm;
import com.blog.service.service.UserService;
import com.blog.check.result.Result;
import org.apache.http.HttpStatus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 用户控制器
 *
 * @author 泡面ing
 * @date 2021/11/17
 */
@Controller
@RequestMapping("/user")
public class UserController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result list(@UnderlineToCamel UserQuery userQuery, HttpServletResponse response){
        Result result;
        try{
            result = userService.list(1,10,userQuery);

        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }

        return result;
    }
    @RequestMapping( value = "/{user_id}",method = RequestMethod.GET)
    @ResponseBody
    public Result get(@PathVariable("user_id") String userId, HttpServletResponse response, HttpServletRequest request){
        Result result;
        try {
            request.getAuthType();
            result = userService.get(userId,request);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody UserForm userForm,HttpServletResponse response) throws Exception {
        Result result;
        try{
            result = userService.save(userForm);
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("保存信息异常",e);
        }
        return result;
    }

    /**
     * 更新
     *
     * @param userForm 用户表单
     * @param response 响应
     * @return {@link Result}
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestBody UserForm userForm,
                         HttpServletResponse response){
        Result result;
        try{
            result = userService.update(userForm);

            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_OK);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("更新信息异常",e);
        }
        return result;
    }

    @RequestMapping(value = "/{username}/enabled", method = RequestMethod.PUT)
    @ResponseBody
    public Result enable(@PathVariable("username") String username,String disableMessage,HttpServletResponse response){
        Result result;
        try{

            result = userService.changeDisable(username, false, disableMessage);

            if (result.getCode() != Result.SUCCESS){
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_OK);
        }catch(Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" +e.getMessage());
            logger.error("启用异常",e);
        }
        return result;
    }

    @RequestMapping(value = "/{username}/disabled", method = RequestMethod.PUT)
    @ResponseBody
    public Result disable(@PathVariable("username") String username,@RequestBody String disableMessage,HttpServletResponse response){
        Result result;
        try{

            result = userService.changeDisable(username, true, disableMessage);

            if (result.getCode() != Result.SUCCESS){
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_OK);
        }catch(Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" +e.getMessage());
            logger.error("启用异常",e);
        }
        return result;
    }
    @RequestMapping(value = "changePassword", method = RequestMethod.POST)
    @ResponseBody
    public Result changePassword(@RequestBody UserForm userForm,HttpServletResponse response) {
        Result result;

        try {
            result = userService.changePassword(userForm);
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("启用异常", e);
        }
        return result;
    }
}